//
//  SEGSessionMessageUserDetailViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/8.
//

#import "SEGSessionMessageUserDetailViewModel.h"
#import "SEGHTTPBusinessManager.h"

@implementation SEGSessionMessageUserDetailViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createCommand];
    }
    return self;
}
- (void)seg_createCommand {
    @weakify(self);
    self.command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *userID) {
        @strongify(self);
        NSDictionary *params = @{@"userId":userID};
        NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/userInfo/userMailDetail.json",nil];
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                NSMutableDictionary *responseDic = [NSMutableDictionary dictionaryWithDictionary:result];
                if (!isSuccessed) {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                    return;
                }
                if (![responseDic[@"data"] isKindOfClass:[NSDictionary class]]) {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"网络不太好，请稍后继续"}];
                    [subscriber sendError:error];
                    return;
                }

                self.userDict = responseDic[@"data"];
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

@end
